// Copyright 2022 a.o.smith ALL Rights Reserved
package com.liuxinlong.modules.project.controller;

import com.liuxinlong.annotation.LogAnnotation;
import com.liuxinlong.constant.LogBusinessConstants;
import com.liuxinlong.constant.LogOperateConstants;
import com.liuxinlong.enums.FileBusinessTypeEnum;
import com.liuxinlong.modules.common.controller.request.BaseDeleteReq;
import com.liuxinlong.modules.common.controller.request.QueryBaseReq;
import com.liuxinlong.modules.common.controller.response.BaseResp;
import com.liuxinlong.modules.entity.Project;
import com.liuxinlong.modules.out.service.OutEcnService;
import com.liuxinlong.modules.out.service.OutProjectService;
import com.liuxinlong.modules.project.controller.request.MergingProjectReq;
import com.liuxinlong.modules.project.controller.request.QueryProjectReq;
import com.liuxinlong.modules.project.controller.request.SyncProjectReq;
import com.liuxinlong.modules.project.controller.request.UpdateProjectMemberReq;
import com.liuxinlong.modules.project.controller.request.UpdateProjectReq;
import com.liuxinlong.modules.project.service.ProjectService;
import com.liuxinlong.modules.system.service.FileManageService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartRequest;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 项目信息管理控制层
 *
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2024年2月22日
 */
@RestController
@RequestMapping("/project")
public class ProjectController {
    @Autowired
    private ProjectService projectService;

    @Autowired
    private OutProjectService outProjectService;

    @Autowired
    private OutEcnService outEcnService;

    @Autowired
    private FileManageService fileManageService;

    /**
     * 分页查询项目信息
     *
     * @param req 请求信息
     * @return 项目信息列表
     */
    @PostMapping("page")
    public BaseResp pageProjectInfo(@RequestBody QueryProjectReq req) {
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("startNum", req.getStartNum());
        queryMap.put("pageSize", req.getPageSize());
        queryMap.put("projectName", req.getProjectName());
        queryMap.put("projectType", req.getProjectType());
        queryMap.put("projectSn", req.getProjectSn());
        queryMap.put("projectFinancialSn", req.getProjectFinancialSn());
        queryMap.put("projectManager", req.getProjectManager());
        queryMap.put("projectLevel", req.getProjectLevel());
        queryMap.put("status", req.getStatus());
        queryMap.put("department", req.getDepartment());
        queryMap.put("dealStatus", req.getDealStatus());
        queryMap.put("startDate", req.getStartDate());
        queryMap.put("endDate", req.getEndDate());
        queryMap.put("projectSource",req.getProjectSource());
        List<Map<String, Object>> dataList = projectService.pageProjectInfo(queryMap);
        Map<String, Object> dataMap = projectService.queryProjectSummary();
        int totalNum = projectService.getProjectCount(queryMap);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("分页查询项目信息成功");
        resp.setDataMap(dataMap);
        resp.setDataList(dataList);
        resp.setTotalNum(totalNum);
        return resp;
    }

    /**
     * 查询目标项目信息
     *
     * @param req 请求信息
     * @return 项目信息列表
     */
    @PostMapping("out-get")
    public BaseResp getProjectInfo(@RequestBody QueryProjectReq req) {
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("startNum", 0);
        queryMap.put("pageSize", 1000);
        queryMap.put("projectSn", req.getProjectSn());
        queryMap.put("projectName", req.getProjectName());
        queryMap.put("projectManager", req.getProjectManager());
        List<Map<String, Object>> dataHTPList = outProjectService.queryProjectList(queryMap);
        List<Map<String, Object>> dataECNList = outEcnService.queryProjectList(queryMap);
        List<Map<String, Object>> dataList = new ArrayList<>();
        dataList.addAll(dataHTPList);
        dataList.addAll(dataECNList);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("查询目标项目信息成功");
        resp.setDataList(dataList);
        return resp;
    }

    /**
     * 新增项目信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("add")
    @LogAnnotation(value = "新增项目信息", business = LogBusinessConstants.PROJECT_MANAGE, operateType = LogOperateConstants.OPERATE_ADD)
    public BaseResp addProjectInfo(@RequestBody UpdateProjectReq req) {
        Project project = new Project();
        project.setProjectName(req.getProjectName());
        project.setSubProjectName(req.getSubProjectName());
        project.setProjectSn(req.getProjectSn());
        project.setProjectFinancialSn(req.getProjectFinancialSn());
        project.setProjectLevel(req.getProjectLevel());
        project.setDepartment(req.getDepartment());
        project.setProjectManager(req.getProjectManager());
        project.setProjectType(req.getProjectType());
        project.setStatus(req.getStatus());
        project.setStartDate(req.getStartDate());
        project.setEndDate(req.getEndDate());
        project.setCloseDate(req.getCloseDate());
        project.setRemark(req.getRemark());
        projectService.addProjectInfo(project);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("新增项目信息成功");
        return resp;
    }

    /**
     * 修改项目信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("update")
    @LogAnnotation(value = "更新项目信息", business = LogBusinessConstants.PROJECT_MANAGE, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp updateProjectInfo(@RequestBody UpdateProjectReq req) {
        Project project = new Project();
        project.setId(req.getId());
        project.setProjectName(req.getProjectName());
        project.setSubProjectName(req.getSubProjectName());
        project.setProjectSn(req.getProjectSn());
        project.setProjectFinancialSn(req.getProjectFinancialSn());
        project.setProjectLevel(req.getProjectLevel());
        project.setProjectType(req.getProjectType());
        project.setDepartment(req.getDepartment());
        project.setProjectManager(req.getProjectManager());
        project.setStatus(req.getStatus());
        project.setStartDate(req.getStartDate());
        project.setEndDate(req.getEndDate());
        project.setCloseDate(req.getCloseDate());
        project.setRemark(req.getRemark());
        projectService.updateProjectInfo(project);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("修改项目信息成功");
        return resp;
    }

    /**
     * 删除项目信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("delete")
    @LogAnnotation(value = "删除项目信息", business = LogBusinessConstants.PROJECT_MANAGE, operateType = LogOperateConstants.OPERATE_DELETE)
    public BaseResp deleteProjectInfo(@RequestBody BaseDeleteReq req) {
        projectService.deleteProjectInfo(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("删除项目信息成功");
        return resp;
    }

    /**
     * 删除项目信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("merging")
    @LogAnnotation(value = "合并项目信息", business = LogBusinessConstants.PROJECT_MANAGE, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp mergingProjectInfo(@RequestBody MergingProjectReq req) {
        projectService.mergingProjectInfo(req);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("合并项目信息成功");
        return resp;
    }

    /**
     * 修改项目处理状态信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("status-update")
    @LogAnnotation(value = "修改项目处理状态信息", business = LogBusinessConstants.PROJECT_MANAGE, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp updateProjectDealStatus(@RequestBody UpdateProjectReq req) {
        projectService.updateProjectDealStatus(req.getId(), req.getDealStatus());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("修改项目处理状态信息成功");
        return resp;
    }

    /**
     * 导出项目信息
     *
     * @param req 请求信息
     * @return 导出数据
     */
    @PostMapping("export")
    @LogAnnotation(value = "导出项目信息", business = LogBusinessConstants.PROJECT_MANAGE, operateType = LogOperateConstants.OPERATE_EXPORT)
    public void exportProjectInfo(@RequestBody QueryProjectReq req, HttpServletResponse response) {
        if (StringUtils.isNotEmpty(req.getId())) {
            projectService.exportProjectInfo(req.getId(), response);
        } else {
            Map<String,Object> queryMap = new HashMap<>();
            queryMap.put("startNum", 0);
            queryMap.put("pageSize", 10000);
            queryMap.put("projectName", req.getProjectName());
            queryMap.put("projectType", req.getProjectType());
            queryMap.put("projectSn", req.getProjectSn());
            queryMap.put("projectFinancialSn", req.getProjectFinancialSn());
            queryMap.put("projectManager", req.getProjectManager());
            queryMap.put("projectLevel", req.getProjectLevel());
            queryMap.put("status", req.getStatus());
            queryMap.put("department", req.getDepartment());
            queryMap.put("dealStatus", req.getDealStatus());
            queryMap.put("startDate", req.getStartDate());
            queryMap.put("endDate", req.getEndDate());
            queryMap.put("projectSource",req.getProjectSource());
            projectService.exportProjectList(queryMap,response);
        }
    }

    /**
     * 查询项目成员信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("member")
    public BaseResp queryMemberInfos(@RequestBody BaseDeleteReq req) {
        List<Map<String, Object>> dataList = projectService.queryMemberInfos(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("查询项目成员信息成功");
        resp.setDataList(dataList);
        return resp;
    }

    /**
     * 查询项目详情信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("detail")
    public BaseResp queryProjectDetail(@RequestBody BaseDeleteReq req) {
        Map<String, Object> dataMap = projectService.queryProjectDetail(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("查询项目详情信息成功");
        resp.setDataMap(dataMap);
        return resp;
    }

    /**
     * 修改项目成员信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("member-update")
    @LogAnnotation(value = "更新项目成员信息", business = LogBusinessConstants.PROJECT_MANAGE, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp updateProjectMemberInfo(@RequestBody UpdateProjectMemberReq req) {
        projectService.updateProjectMemberInfo(req.getProjectId(), req.getProjectMember(), req.getOperateType());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("更新项目成员信息成功");
        return resp;
    }

    /**
     * 查询项目文件信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("file-query")
    public BaseResp queryFileInfos(@RequestBody QueryBaseReq req) {
        Map<String, Object> queryParam = new HashMap<>();
        queryParam.put("businessId", req.getId());
        queryParam.put("businessType", FileBusinessTypeEnum.PROJECT_FILE.getValue());
        queryParam.put("startNum", req.getStartNum());
        queryParam.put("pageSize", req.getPageSize());
        List<Map<String, Object>> dataList = fileManageService.queryFileList(queryParam);
        int totalNum = fileManageService.getFileCount(queryParam);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("查询项目文件信息成功");
        resp.setDataList(dataList);
        resp.setTotalNum(totalNum);
        return resp;
    }

    /**
     * 上传项目文件
     *
     * @param multipartRequest 请求信息
     * @return 成功标识
     */
    @PostMapping("file-add")
    @LogAnnotation(value = "上传项目文件", business = LogBusinessConstants.PROJECT_MANAGE, operateType = LogOperateConstants.OPERATE_IMPORT)
    public BaseResp addFileInfos(MultipartRequest multipartRequest) {
        projectService.addFileInfos(multipartRequest);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("上传项目文件成功");
        return resp;
    }

    /**
     * 删除项目文件
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("file-delete")
    @LogAnnotation(value = "删除项目文件", business = LogBusinessConstants.PROJECT_MANAGE, operateType = LogOperateConstants.OPERATE_DELETE)
    public BaseResp deleteProjectFile(@RequestBody BaseDeleteReq req) {
        fileManageService.deleteFileManage(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("删除项目文件成功");
        return resp;
    }

    /**
     * 下载项目文件
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("file-download")
    @LogAnnotation(value = "下载项目文件", business = LogBusinessConstants.PROJECT_MANAGE, operateType = LogOperateConstants.OPERATE_EXPORT)
    public void downloadProjectFile(@RequestBody BaseDeleteReq req, HttpServletResponse response) {
        projectService.downloadProjectFile(req.getId(), response);
    }

    /**
     * 同步项目信息
     *
     * @param req 请求信息
     * @return 项目信息列表
     */
    @PostMapping("sync")
    public BaseResp syncProjectInfo(@RequestBody SyncProjectReq req) {
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("startDate", req.getStartDate());
        queryMap.put("endDate", req.getEndDate());
        projectService.syncProjectInfo(queryMap, 0, req.getProjectSource());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("同步项目信息成功");
        return resp;
    }
}
